#!/bin/bash

set -e          # exit on error
set -o pipefail # exit on pipeline error
set -u          # treat unset variable as error

. /minioslib
. /minios-build.conf

declare_locales

SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
[ "${DISTRIBUTION}" = "bionic" ] && PACKAGE="firefox" || PACKAGE="firefox-esr"

pkg update

# Conditional statements for package installation
if [ "${DISTRIBUTION_TYPE}" = "ubuntu" ]; then
    if [ "${DISTRIBUTION}" = "jammy" ] || [ "${DISTRIBUTION}" = "noble" ]; then
        pkg install software-properties-common
        add-apt-repository ppa:mozillateam/ppa
        cat <<EOF >/etc/apt/preferences.d/mozilla-firefox
Package: *
Pin: release o=LP-PPA-mozillateam
Pin-Priority: 1001
EOF
    fi
fi

pkg install "${PACKAGE}"

# Function for locale package installation
install_locale_package() {
    local DISTRIBUTION_TYPE=$1
    local PACKAGE=$2
    local LOCALE_CONFIG=$3

    IFS=',' read -ra CONFIG <<<"${LOCALE_CONFIG}"
    local PACKAGE_LOCALE_DEBIAN=${CONFIG[2]}
    local PACKAGE_LOCALE_UBUNTU=${CONFIG[3]}

    if [ "$DISTRIBUTION_TYPE" = "ubuntu" ] && [ -n "${PACKAGE_LOCALE_UBUNTU}" ]; then
        pkg install "${PACKAGE}-locale-${PACKAGE_LOCALE_UBUNTU}"
    elif [ "$DISTRIBUTION_TYPE" = "debian" ] && [ -n "${PACKAGE_LOCALE_DEBIAN}" ]; then
        pkg install "${PACKAGE}-l10n-${PACKAGE_LOCALE_DEBIAN}"
    fi
}

# Form INSTALL_LOCALES from LOCALES or from LOCALE
INSTALL_LOCALES=("${!LOCALES[@]}")
[[ "${MULTILINGUAL}" = "false" ]] && INSTALL_LOCALES=("${LOCALE}")

for locale in "${INSTALL_LOCALES[@]}"; do
    if [[ ! -v LOCALES[${locale}] ]]; then
        echo "Locale ${locale} is not supported, skipping."
        continue
    fi

    LOCALE_CONFIG="${LOCALES[${locale}]}"
    if [ -z "${LOCALE_CONFIG//,/}" ]; then
        echo "No configuration available for locale ${locale}, skipping."
        continue
    fi

    install_locale_package "${DISTRIBUTION_TYPE}" "${PACKAGE}" "${LOCALE_CONFIG}"
done

# Conditional Desktop environment settings
if [[ "${DESKTOP_ENVIRONMENT}" == *"xfce"* ]] && [ "${PACKAGE_VARIANT}" != "puzzle" ]; then
    mkdir -p /etc/skel/.config/xfce4
    if grep -q "xterm" /etc/skel/.config/xfce4/helpers.rc; then
        cat <<EOF >/etc/skel/.config/xfce4/helpers.rc
TerminalEmulator=xterm
FileManager=Thunar
WebBrowser=firefox
EOF
    elif grep -q "xfce4-terminal" /etc/skel/.config/xfce4/helpers.rc; then
        cat <<EOF >/etc/skel/.config/xfce4/helpers.rc
TerminalEmulator=xfce4-terminal
FileManager=Thunar
WebBrowser=firefox
EOF
    fi
fi

if [[ "${DESKTOP_ENVIRONMENT}" == *"lxqt"* ]] && [ -f /etc/skel/.config/lxqt/panel.conf ]; then
    sed -i "46iapps\\3\\desktop=/usr/share/applications/${PACKAGE}.desktop" /etc/skel/.config/lxqt/panel.conf
fi

echo "application/pdf=${PACKAGE}.desktop" >>/usr/share/applications/mimeapps.list

if [ "${DISTRIBUTION_TYPE}" = "debian" ] && [[ "${DESKTOP_ENVIRONMENT}" == *"xfce"* ]]; then
    sed -i "s,^Exec=/usr/lib,Exec=env XDG_CURRENT_DESKTOP=XFCE /usr/lib,g" /usr/share/applications/${PACKAGE}.desktop
elif [ "${DESKTOP_ENVIRONMENT}" == "flux" ]; then
    FILES="/usr/share/applications/firefox.desktop /usr/share/applications/firefox-esr.desktop"
    for FILE in ${FILES}; do
        if [ -f "${FILE}" ]; then
            rm -f ${FILE}
        fi
    done
fi

echo "PACKAGE=\"${PACKAGE}\"" >/.package
echo "VERSION=\"$(dpkg -s "${PACKAGE}" | grep Version | sed 's/Version: //g' | sed 's/:/-/g')\"" >>/.package
